package com.yuncheng.oaApi.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.entity.OaLgqs;
import com.yuncheng.oaApi.mapper.OaLgqsMapper;
import com.yuncheng.oaApi.service.IOaLgqsService;
import com.yuncheng.utils.ThreadContextHelper;
import com.yuncheng.vo.CurrentUserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;

import java.util.List;

/**
 * @Description: 离岗请示审批管理
 * @Author: yuncheng
 * @Date: 2023-06-26
 * @Version: V1.0
 */
@Service
public class OaLgqsServiceImpl extends BaseServiceImpl<OaLgqsMapper, OaLgqs> implements IOaLgqsService {

    @Override
    public List<OaLgqs> getList(OaLgqs lgqs, String year, boolean isSelectMe) {
        LambdaQueryWrapper<OaLgqs> queryWrapper = new LambdaQueryWrapper<>();
        //根据条件筛选导出
        //用户id
        queryWrapper.eq(StringUtils.isNotBlank(lgqs.getFuserid()),OaLgqs::getFuserid,lgqs.getFuserid());
        //填报部门
        queryWrapper.eq(StringUtils.isNotBlank(lgqs.getFtbbm()),OaLgqs::getFtbbm, lgqs.getFtbbm());
        //姓名
        queryWrapper.like(StringUtils.isNotEmpty(lgqs.getFxm()),OaLgqs::getFxm,lgqs.getFxm());
        //职务
        queryWrapper.like(StringUtils.isNotEmpty(lgqs.getFzw()),OaLgqs::getFzw,lgqs.getFzw());
        //离岗类型
        queryWrapper.like(StringUtils.isNotEmpty(lgqs.getFlglx()),OaLgqs::getFlglx,lgqs.getFlglx());
        //报送时间
        queryWrapper.like(StringUtils.isNotEmpty(lgqs.getFbssj()),OaLgqs::getFbssj,lgqs.getFbssj());

        if (lgqs.getFlgsj() != null){
            queryWrapper.le(OaLgqs::getFlgsj,lgqs.getFlgsj());
        }
        if (lgqs.getFhgsj() != null){
            queryWrapper.ge(OaLgqs::getFhgsj,lgqs.getFhgsj());
        }
        if (StringUtils.isNotBlank(year)){
            queryWrapper.apply("to_char(flgsj,'yyyy') = {0}",year);
        }
        if (isSelectMe){
            CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();
            if (currentUser != null){
                queryWrapper.eq(StringUtils.isNotBlank(currentUser.getId()),OaLgqs::getFuserid,currentUser.getId());
            }
        }
        queryWrapper.orderByDesc(OaLgqs::getFlgsj);
        List<OaLgqs> pageList = list(queryWrapper);
        return pageList;
    }

}
